

import * as Utils from '@src/component/form/util';
import { isOpenData } from '@src/util/platform';

import * as TaskApi from '@src/api/TaskApi.ts';
import * as CustomerApi from '@src/api/CustomerApi.ts';

export const TextFormType = ['text', 'textarea', 'code', 'relationProduct', 'relationCustomer'];
export const DateFormType = ['date', 'datetime', 'planTime', 'planStartTime', 'planEndTime'];
import i18n from '@src/locales';
const i18nMap = {
  '包含': i18n.t('common.base.include'),
  '等于': i18n.t('common.base.equal'),
  '介于': i18n.t('common.base.between')
}

export const OperatorSelectOptionsMap = {
  // 客户产品 数字只有等于 没做大于小于之类的
  input: [
    // { label: '包含', value: 'like' },
    { label: i18nMap['等于'], value: 'eq' },
    // { label: '大于', value: 'gt' },
    // { label: '大于等于', value: 'ge' },
    // { label: '小于', value: 'lt' },
    // { label: '小于等于', value: 'le' },
  ],
  text: [{ label: i18nMap['包含'], value: 'like' }, { label: i18nMap['等于'], value: 'eq' }],
  multiple: [{ label: i18nMap['包含'], value: 'in' }], //  { label: '不包含', value: 'not_in' }
  description: [{ label: i18nMap['包含'], value: 'like' }],
  date: [{ label: i18nMap['介于'], value: 'between' }],
  select: [{ label: i18nMap['等于'], value: 'eq' }],
  cascader: [{ label: i18nMap['包含'], value: 'cascader' }] // , { label: '不包含', value: 'not_in' }
};

// 产品编号、二维码编号目前只支持精确搜索，不支持模糊搜索
export const eqFieldNames = ['serialNumber', 'qrcodeId'];

export const MultiFieldNames = [
  'serviceType',
  'serviceContent',
  'level',
  'paymentMethod',
  'state',
  'allotTypeStr',
  'onceException',
  'allotUser',
  'tags',
  'synergyId',
  'createUser',
  'executor',
  'source', // 创建方式
];

// 根据不同formType 返回对应的 operator
export function matchOperator(field) {
  let formType = field.formType;
  let operator = '';

  switch (formType) {
    case 'date': {
      operator = 'between';
      break;
    }
    case 'datetime': {
      operator = 'between';
      break;
    }
    case 'select': {
      if (field.setting && field.setting.isMulti) {
        operator = 'contain';
      } else {
        operator = 'eq';
      }
      break;
    }
    case 'user': {
      operator = 'user';
      break;
    }
    case 'address': {
      operator = 'address';
      break;
    }
    case 'cascader': {
      operator = 'cascader';
      break;
    }
    case 'location': {
      operator = 'location';
      break;
    }
    case 'related_task': {
      operator = 'array_eq';
      break;
    }
    case 'formula': {
      operator = 'eq';
      break;
    }
    case 'related_customers': {
      operator = 'in';
      break;
    }
    default: {
      operator = 'like';
      break;
    }
  }
  return operator;
}

// 设置operatorOptions && operatorValue
export function setFieldOperateHandler(field = {}) {
  let operateOption = 'select';
  const { fieldName, formType } = field;
  const isMulti = Utils.isMultiField(field);

  if (formType == 'number') { // 数字
    operateOption = 'input';
  }
  else if (MultiFieldNames.includes(fieldName) && isMulti) { // 多选字段
    operateOption = 'multiple';
  }
  else if (formType === 'description' || fieldName === 'eventNo') { // 描述
    operateOption = 'description';
  }
  else if (TextFormType.includes(formType)) { // 文本
    operateOption = eqFieldNames.includes(fieldName) ? 'select' : 'text';
  }
  else if (DateFormType.includes(formType)) { // 日期
    operateOption = 'date';
  }
  else if (formType == 'select') { // 下拉列表
    operateOption = isMulti ? 'multiple' : 'select';
  }
  else if (formType === 'cascader') { // 多级菜单
    operateOption = 'cascader';
  }
  else if(formType === 'related_customers') { // 关联客户
    operateOption = 'multiple';
  }
  else {
    operateOption = isMulti ? 'multiple' : 'select';
  }

  field.operatorOptions = OperatorSelectOptionsMap[operateOption].slice();
  field.operatorValue = field.operatorOptions[0].value;
}

// 远程搜索客户接口
export function searchCustomer(params) {
  const pms = params || {};

  return CustomerApi.getCustomerListAsyn(pms)
    .then(res => {
      if (!res || !res.list) return;

      res.list = res.list.map(customer =>
        Object.freeze({
          label: customer.name,
          value: customer.id,
          ...customer,
        })
      );
      return res;
    })
    .catch(e => console.error(e));
}

// 远程搜索产品接口
export function searchProduct(params) {
  const pms = params || {};

  pms.customerId = this.form.customer || '';
  return TaskApi.getTaskCustonerProductList(pms)
    .then(res => {
      if (!res.result || !res.result?.list) return;
      res.result.list = res.result.list.map(product =>
        Object.freeze({
          label: product.name,
          value: product.id,
          ...product,
        })
      );
      return res.result;
    })
    .catch(e => console.error(e));
}



/** 客户 */
export async function getSimpleCustomerList(params, fn) {
  const { result } = await TaskApi.getSimpleCustomerList(params);
  fn(result[0]);
}

/** 服务团队 */
export async function getSimpleTagListByIds(params, fn) {
  const { result } = await TaskApi.getSimpleTagListByIds(params);
  fn(result);
}

/** 用户 */
export async function getSimpleUserListByIds(params, fn) {
  const { result } = await TaskApi.getSimpleUserListByIds(params);
  fn(
    isOpenData && result
      ? result.map(item => item.staffId)
      : result.map(item => item.displayName).join('，')
  );
}

/** 产品 */
export async function getSimpleProductList(params, fn) {
  const { result } = await TaskApi.getSimpleProductList(params);
  fn(result[0]);
}

/** 联系人 */
export async function getLinkmanListByIds(params, fn) {
  const { result } = await TaskApi.getLinkmanListByIds(params);
  fn(result[0]);
}